package com.tristia.service;

import java.io.IOException;
import java.net.URL;

import org.apache.log4j.Logger;

import com.google.gdata.client.books.BooksService;
import com.google.gdata.client.books.VolumeQuery;
import com.google.gdata.data.books.VolumeFeed;
import com.google.gdata.util.ServiceException;

public class GoogleBooksService {
	
	String appName;
	BooksService booksService;
	long isbn = 0;
	
	private Logger log = Logger.getLogger(this.getClass());

	public GoogleBooksService() {
		super();
		// TODO Auto-generated constructor stub
	}
	
	public GoogleBooksService(String appName) {
		this.appName = appName;
		this.booksService = new BooksService(appName);
	}
	
/*	public GoogleBooksService(BooksService booksService, long isbn) {
		this.booksService = booksService;
		this.isbn = isbn;
		// TODO Auto-generated constructor stub
	}*/

	/**
	 * The name of the server hosting the YouTube GDATA feeds.
	 */
	public static final String BOOKS_GDATA_SERVER = "http://books.google.com";


	/**
	 * The URL of the volumes feed
	 */
	public static final String VOLUMES_FEED = BOOKS_GDATA_SERVER
	+ "/books/feeds/volumes";

	/**
	 * The URL of the user library feeds
	 */
	public static final String USER_LIBRARY_FEED = BOOKS_GDATA_SERVER
	+ "/books/feeds/users/me/collections/library/volumes";

	/**
	 * The URL of the user annotation feed
	 */
	public static final String USER_ANNOTATION_FEED = BOOKS_GDATA_SERVER
	+ "/books/feeds/users/me/volumes";

	//	  BooksService service = new BooksService("gdataSample-Books-1");

	/**
	 * Searches the VOLUMES_FEED for search terms and print each resulting
	 * VolumeEntry.
	 *
	 * @param service a BooksService object.
	 * @param authenticated whether the user is authenticated.
	 * @throws ServiceException
	 *                     If the service is unable to handle the request.
	 * @throws IOException error sending request or reading the feed.
	 */
	public VolumeFeed searchVolumesByISBN(long isbn) throws IOException, ServiceException {
//	    VolumeQuery query = new VolumeQuery(new URL(VOLUMES_FEED));
		VolumeQuery query = new VolumeQuery(new URL(VOLUMES_FEED+"?q=ISBN<"+isbn+">"));
//		VolumeQuery query = new VolumeQuery(new URL(VOLUMES_FEED+"/8N9QAAAAMAAJ"));

		log.info("Hit searchVolumesByISBN");

		VolumeFeed volumeFeed = booksService.query(query, VolumeFeed.class);
		
		log.info("Get volumeFeed");

		return volumeFeed;
	}
}
